package com.jackeysun.app_common

import android.content.res.Resources
import android.graphics.Color
import android.os.Bundle
import android.text.SpannableStringBuilder
import android.text.method.LinkMovementMethod
import android.view.Menu
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.jackeysun.app_common.databinding.ActivityMainBinding
import com.jackeysun.common.*
import com.jackeysun.common.text.*
import com.jackeysun.util.switchThread
import com.jackeysun.util.toUI
import com.jackeysun.util.toast
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch


class MainActivity : AppCompatActivity() {

    companion object {
        val TAG = MainActivity::class.java.simpleName
    }

//    val Context.bookDataStorePf: DataStore<Preferences> by
//    preferencesDataStore(name = "pf_datastore")// 文件名称
//    private lateinit var dataStore: DataStore<Preferences>
//    private var mSelectableTextHelper: SelectableTextHelper? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val mainBinding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(mainBinding.root)

        val Sbp = SpannableStringBuilder()
        Sbp.append("sssssssss")
        Sbp.insert(0, "iiiii")

        Sbp.insert(Sbp.length, "hhhh")

        println(Sbp)
        lifecycleScope.launch {
//            mainBinding.tv.showRich(
//                DataSource.img3/*, "img", ImageHandlerSelf(
//                    this@MainActivity,
//                    Resources.getSystem().displayMetrics.widthPixels
//                )*/
//            )
//            mainBinding.tv.showRich(
//                DataSource.img3, "img", ImageClickHandler(this@MainActivity)
//            )
            mainBinding.tv2.showRich(
                DataSource.html2, "img", ImageHandlerSelf(
                    this@MainActivity,
                    Resources.getSystem().displayMetrics.widthPixels
                )
            )

            mainBinding.tv.buildSpannableString {
                addText("我已详细阅读并同意")
                addText("《隐私政策》") {
                    setColor("#0099FF")
                    onClick {
                        "click".toast()
                    }
                }
            }

            mainBinding.tv2.movementMethod = LinkMovementMethod.getInstance()
//            mainBinding.tv2.text = DataSource.data
//            mainBinding.tv2.onSelectListener = object : OnSelectListener {
//                override fun onSelected(selectText: CharSequence?) {
//                    Toast.makeText(this@MainActivity, selectText, Toast.LENGTH_LONG).show()
//                }
//            }
//            mainBinding.tv2.floatWindow = CustomPopWindow(this@MainActivity)
//            TextViewSelectable.Builder()
//                .setTextView(mainBinding.tv2)
//                .build()
            //mTvTest.setTextIsSelectable(true);
//            mSelectableTextHelper = SelectableTextHelper.Builder(mainBinding.tv2)
//                .setSelectedColor(Color.parseColor("#0d7aff"))
//                .build()
//
//            mSelectableTextHelper?.setSelectListener {
//                Toast.makeText(this@MainActivity, it, Toast.LENGTH_LONG).show()
//            }

        }
    }


    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_util, menu)
        return super.onCreateOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == R.id.recyclerview) {
            go(MainActivity2()){

            }
            goto(MainActivity2()){
                this.putExtra("","")
            }
//            startActivity(Intent(this@MainActivity, MainActivity2::class.java))
            return true
        }
        if (item.itemId == R.id.activityManager) {
            go(MainActivity3())
//            startActivity(Intent(this@MainActivity, MainActivity3::class.java))
            return true
        }
        if (item.itemId == R.id.pdf) {
            go(MainActivity5())
            return true
        }
        if (item.itemId == R.id.html) {
            go(MainActivity6())
            return true
        }
        return super.onOptionsItemSelected(item)
    }
}